#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2019/2/14
# @Author    :Tian Maohong
# @Email     :tianmaohong@bbdservice.com
# @File      :R48_TX_EntIllegalStatusBadTaxCheck_GZ
# @Software  :PyCharm


import json
import re
import sys
import datetime

'''eof
name:企业纳税违法违章状态校验
code:R48_TX_EntIllegalStatusBadTaxCheck_GZ
tableName:
columnName:
groups:场景业务校验-税务
dependencies:TX_GZ_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


# reload(sys)
# sys.setdefaultencoding('utf-8')


def inquireIsReportNo(data):
    """
    判断是否存在reportNo
    :param data:
    :return:True/False
    """
    try:

        is_reportNo = True if len(data.get("reportNo")) > 0 else False

    except:
        is_reportNo = False

    return is_reportNo


def formatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        data = TX_GZ_DSJ["data"].get(table_Name)
        return data if isinstance(data, list) and len(data) > 0 else [{}]

    except:
        return [{}]


def convertDataType(data_value, data_type):
    """
    数据格式转换
    :param data_value:
    :param data_type: float/int/str/date_time
    :return:
    """
    data_value = str(data_value)
    return_data = None
    # float
    if data_type == 'float':
        try:
            return_data = float(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # int
    elif data_type == 'int':
        try:
            return_data = int(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # str
    elif data_type == 'str':
        try:
            return_data = str(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # date_time
    elif data_type == 'date_time':
        r = re.compile(r'\D+')
        try:
            return_data = datetime.datetime.strptime(data_value, '%Y.%m.%d').strftime('%Y-%m-%d')
        except:
            try:
                return_data = datetime.datetime.strptime(data_value, '%Y-%m-%d').strftime('%Y-%m-%d')
            except:
                try:
                    return_data = datetime.datetime.strptime(data_value, '%Y/%m/%d').strftime('%Y-%m-%d')
                except:
                    try:
                        return_data = datetime.datetime.strptime(data_value, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')
                    except:
                        try:
                            data_value = r.sub('', data_value)
                            return_data = datetime.datetime.strptime(data_value, '%Y%m%d').strftime('%Y-%m-%d')
                        except:
                            return_data = None

    return return_data


def entIllegalStatusBadTaxCheck():
    """
    企业不良纳税记录校验（违法违章状态）
    :return:
    """
    syptWfwzxx = formatData('syptWfwzxx')
    CcrDay = datetime.datetime.now().date()
    StartDay = CcrDay - datetime.timedelta(days=30 * 3)
    # PassList = [u'登记待处理', u'责令限期改正', u'责令限期缴纳税款', u'调查巡查',
    #             u'税务行政处罚事项告知处理', u'税务行政处罚听证', u'简易程序处罚',
    #             u'一般程序处罚决定处理', u'延（分）期缴纳罚款申请审批', u'查补税款和罚款变更或补充处理',
    #             u'税务行政复议', u'复议终止处理', u'税务行政应诉', u'税务机关上诉处理',
    #             u'税务机关申诉与请求抗诉处理', u'稽查立案审批', u'稽查案件检查',
    #             u'稽查案件审理', u'解除保全', u'解除停供（收缴）发票', u'现金扣缴', u'强制扣缴',
    #             u'拍卖变卖', u'申请强制执行', u'行使代位（撤销）权', u'布控', u'撤控',
    #             u'涉税事项内部移送', u'征收开票', u'处理完毕', u'其他']

    PassDM_List = [u'01', u'02', u'03', u'04', u'05', u'06', u'07', u'08',
                   u'09', u'11', u'12', u'13', u'14', u'15', u'16', u'17', u'18',
                   u'19', u'24', u'26', u'29', u'30', u'33', u'34', u'35', u'36', u'37', u'38', u'99', u'1', u'2', u'3',
                   u'4', u'5', u'6', u'7', u'8', u'9']

    if syptWfwzxx == [{}]:
        return 1
    else:
        WFXWCLZT_DMList = []
        for i in range(len(syptWfwzxx)):
            LARQ = convertDataType(syptWfwzxx[i].get('LARQ')[:10], 'date_time')
            if LARQ:
                LARQDay = datetime.datetime.strptime(LARQ, '%Y-%m-%d').date()
                if StartDay <= LARQDay and LARQDay <= CcrDay:
                    WFXWCLZT_DM = convertDataType(syptWfwzxx[i].get('WFXWCLZT_DM'), 'str')
                    if WFXWCLZT_DM and WFXWCLZT_DM not in PassDM_List:
                        WFXWCLZT_DMList.append(0)
                    else:
                        WFXWCLZT_DMList.append(1)
                else:
                    WFXWCLZT_DMList.append(1)
            else:
                WFXWCLZT_DMList.append(1)

        # 逻辑判断
        if 0 in WFXWCLZT_DMList:
            Factor = 0
        else:
            Factor = 1

        return Factor


result = entIllegalStatusBadTaxCheck()
